/**
 * 
 */
package fpt.pits.util;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;

/**
 * Get database connection
 * @author yoyosakura
 * @version 1.0
 */
public class Connection {
    /**
     * using ResourceBundle read db.properties file
     */    
    private static ResourceBundle rb = ResourceBundle.getBundle("db");
    
    /**
     * Load the driver and establish a connection
     * @return A connection with database
     */
    public static java.sql.Connection getConnection() {
        try {
            String jdbcDriver = rb.getString(Constants.JDBC_DRIVER);
            String jdbcUrl = rb.getString(Constants.JDBC_URL);
            String jdbcUser = rb.getString(Constants.JDBC_USER);
            String jdbcPassword = rb.getString(Constants.JDBC_PASSWORD);
       
            Class.forName(jdbcDriver);
            return (java.sql.Connection) DriverManager.getConnection(jdbcUrl,
                    jdbcUser, jdbcPassword);
        } catch (ClassNotFoundException cnfe) {
            System.err.println("Error: Driver class is not found " + cnfe.getMessage());
        } catch (SQLException sqle) {
            System.err.println("Error: No connection with database " + sqle.getMessage());
        }       
        return null;
    }
}
